﻿#include "com_manager.h"
#include "plug_info.h"

#include <QApplication>
#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QVector>

PlugInfo * PlugInfo::instance = NULL;

QMap<QString, _ETHERNET > PlugInfo::map_ethernet;
QMap<QString, _485 > PlugInfo::map_485;
QMap<QString, _CAN > PlugInfo::map_can;

PlugInfo::PlugInfo() {}

PlugInfo * PlugInfo::Instance()
{
    if(instance == NULL)
        instance = new PlugInfo;

    return instance;
}

bool PlugInfo::loadSetting()
{
    if (load_port_setting() == false)
    {
     init_port_default(); //initial default
    }

    save_port_Setting();

	return false;
}

bool PlugInfo::load_port_setting()
{
    QFile fileArray(QApplication::applicationDirPath() + "/data/port_setting.json");
    if (fileArray.open(QIODevice::ReadOnly))
    {
        map_ethernet.clear();
    map_485.clear();
    map_can.clear();



    QByteArray bytes = fileArray.readAll();
    QJsonDocument doc = QJsonDocument::fromJson(bytes);

    QJsonObject all = doc.object();
    QJsonObject arrayNet = all.value("ethernet").toObject();
    QJsonObject array485 = all.value("485").toObject();
    QJsonObject arrayCan = all.value("can").toObject();

    QStringList _id = arrayNet.keys();
    // int cnt_net = _id.count();
    foreach (QString key, _id)
    {
        //	QJsonObject  obj = arrayNet[.at(i)]];
        _ETHERNET  log;// new _ETHERNET;
        //	log->fromJson(obj;
        //QStringList _id = key;
        log.ip = arrayNet.value(key).toString();
        map_ethernet[key] = log;
    }

    QStringList key_485 = array485.keys();
    // int cnt_485 = array485.keys().count();
    int i = 0;
    foreach (QString key, key_485)
     {
        QJsonObject  obj = array485.value(key).toObject();

        QString id = QString("485_%1").arg(  i++ + 1,2, 10, QLatin1Char('0'));
        _485  log;// = new _485;
        //	log->fromJson(obj);
        log.com = obj["com"].toString();
        log.baud = obj["baud"].toString();
        log.parity = obj["parity"].toString();
        log.dataBit = obj["dataBit"].toString();
        log.stopBit = obj["stopBit"].toString();
        //	log_info.append(log);

        map_485[id] = log;
    }
    QStringList key_can = arrayCan.keys();
    // int cnt_can = arrayCan.keys().count();
    i = 0;
    foreach (QString key, key_can)
    {
        QJsonObject  obj = arrayCan.value(key).toObject();
        QString id = QString("can_%1").arg(  i++ + 1, 2,10, QLatin1Char('0'));
        _CAN  log;// = new _CAN;
        //	log->fromJson(obj);
        //
        log.com = obj["com"].toString();
        log.baud = obj["baud"].toString();
        log.parity = obj["parity"].toString();
        log.dataBit = obj["dataBit"].toString();
        log.stopBit = obj["stopBit"].toString();
        //	log_info.append(log);
        map_can[id] = log;
    }

    return true;
    }
    return false;
 }

void PlugInfo::init_port_default()
{
    for (int i = 0; i < 24; i++)
    {
        _ETHERNET  log;// new _ETHERNET;
        //	log->fromJson(obj);
        QString  id = QString("ethernet_%1").arg(  i + 1,2, 10, QLatin1Char('0'));
        log.ip = QString("192.168.1.%1").arg(i + 200);
        map_ethernet[id] = log;
    }

    for (int i = 0; i < 66; i++)
    {
        _485 info;
        QString  id = QString("485_%1").arg(  i + 1, 2,10, QLatin1Char('0'));
        info.com = com_manager::instance()->curComName;//>comList.first();
        info.baud = "115200";
        info.dataBit  = "8";
        info.parity= "无";
        info.stopBit = "1";

        map_485[id] = info;
    }

    for (int i = 0; i < 66; i++)
    {
        _CAN can;
        QString  id = QString("can_%1").arg(  i + 1, 2,10, QLatin1Char('0'));
        can.com = com_manager::instance()->curComName;//comList.first();
        can.baud = "115200";
        can.dataBit  = "8";
        can.parity= "无";
        can.stopBit = "1";
        map_can[id] = can;
    }
}

void PlugInfo::save_port_Setting()
{
	QDir dir(qApp->applicationDirPath());
	if (!dir.exists("data"))
		dir.mkpath("data");

	//QJsonArray arrayNet;
	QJsonObject arrayNet;
    QMap<QString, _ETHERNET >::Iterator itNet = PlugInfo::map_ethernet.begin();
    for (; itNet != PlugInfo::map_ethernet.end(); ++itNet)
	{
		//QJsonObject  obj;

		arrayNet[itNet.key()] = itNet->ip;

		//arrayNet.append(obj);
	}

    QJsonObject array485;
    QMap<QString, _485 >::iterator it485 = PlugInfo::map_485.begin();
    int i_485 = 1;
    for (; it485 != PlugInfo::map_485.end(); ++it485)
	{
		QJsonObject  obj;
        obj["com"] = it485->com;
		obj["baud"] = it485->baud;
		obj["parity"] = it485->parity;
		obj["dataBit"] = it485->dataBit;
		obj["stopBit"] = it485->stopBit;

        QString name = QString("485_%1").arg(i_485++, 2, 10, QLatin1Char('0') );
        array485[name] = obj;
	}

    QJsonObject arrayCan;
    QMap<QString, _CAN >::iterator itCan = PlugInfo::map_can.begin();
  int  i_can = 1;
    for (; itCan != PlugInfo::map_can.end(); ++itCan)
	{
		//	QJsonObject  obj = dataArray.at(i).toObject();
		QJsonObject  obj;
		obj["com"] = itCan.value().com;
		obj["baud"] = itCan->baud;
		obj["parity"] = itCan->parity;
		obj["dataBit"] = itCan->dataBit;
		obj["stopBit"] = itCan->stopBit;
        QString name = QString("can_%1").arg(i_can++, 2, 10, QLatin1Char('0') );
        arrayCan[name] = obj;
	}

	QJsonObject content;

    content["ethernet"] = arrayNet;
	content["485"] = array485;
	content["can"] = arrayCan;


	QByteArray bytes = QJsonDocument(content).toJson();

    QFile fileArray(QApplication::applicationDirPath() + "/data/port_setting.json");
	if (fileArray.open(QIODevice::WriteOnly))
	{
		fileArray.write(bytes);
		fileArray.close();
	}
}
